`
https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays/
`

/**
 * @param {number[]} nums
 * @param {number} firstLen
 * @param {number} secondLen
 * @return {number}
 */
var maxSumTwoNoOverlap = function (nums, firstLen, secondLen) {
  // 思路类似于两数之和，但本题枚举的是子数组
  // 类似题目：LC 2555. 两个线段获得的最多奖品
  const n = nums.length
  // nums 的前缀和
  const prefix = nums.reduce((acc, cur) => {
    acc.push(acc.at(-1) + cur)
    return acc
  }, [0])

  // 左 a 右 b 的情况
  const f = (a, b) => {
    let res = 0
    let maxSumA = 0
    for (let i = a + b; i < n + 1; i++) {
      maxSumA = Math.max(maxSumA, prefix[i - b] - prefix[i - b - a])
      res = Math.max(res, maxSumA + prefix[i] - prefix[i - b])
    }
    return res
  }
  return Math.max(f(firstLen, secondLen), f(secondLen, firstLen))
};
